/*
@title: bone_hunt
@description: Bone Hunt is a puzzle game where you play as a dog on a mission to collect all of your bones in each level. Once all bones are collected, you advance to the next level.
@author: Andrew Cromar
@tags: ['puzzle']
@addedOn: 2024-01-15
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const player = "p";
const grass = "g";
const bone = "f";
const enemie = "e";

const fence_vertical = "v";
const fence_horizontal = "h";
const fence_tl_corner = "c";
const fence_tr_corner = "b";
const fence_bl_corner = "n";
const fence_br_corner = "m";

setLegend(
  [ player, bitmap`
................
................
................
.........F......
..........F.....
..........FFF...
..F.......FF0...
..F.......FFFFF.
..FFFFFFFFFFFFF.
....FFFFFFF.....
....FFFFFFF.....
....F.F.F.F.....
....F.F.F.F.....
................
................
................` ],
  [ bone, bitmap `
................
................
................
................
................
...22.....22....
...22.....22....
....2222222.....
....2222222.....
...22.....22....
...22.....22....
................
................
................
................
................` ],
  [ grass, bitmap `
4444444444444444
4444444444444444
4444444444D4D444
44D4D444444D4444
444D444444444444
4444444444444444
4444444444444444
4444444444444444
44444444D4D44444
444444444D444444
4444444444444444
4444444444444444
44D4D44444444444
444D4444444D4D44
444444444444D444
4444444444444444`],
  [ fence_vertical , bitmap `
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......` ],
  [ fence_horizontal , bitmap `
................
................
................
................
................
................
.L.L.L.L.L.L.L.L
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
.L.L.L.L.L.L.L.L
................
................
................
................
................
................` ],
  [ fence_tl_corner , bitmap `
................
................
................
................
................
................
.......L.L.L.L.L
......LLLLLLLLLL
.......LLLLLLLLL
......LLLL.L.L.L
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......` ],
  [ fence_tr_corner , bitmap `
................
................
................
................
................
................
L.L.L.L.L.......
LLLLLLLLLL......
LLLLLLLLL.......
L.L.L.LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......` ],
  [ fence_bl_corner , bitmap `
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL.L.L.L
.......LLLLLLLLL
......LLLLLLLLLL
.......L.L.L.L.L
................
................
................
................
................
................` ],
  [ fence_br_corner , bitmap `
......LLLL......
.......LL.......
......LLLL......
.......LL.......
......LLLL......
.......LL.......
L.L.L.LLLL......
LLLLLLLLL.......
LLLLLLLLLL......
L.L.L.L.L.......
................
................
................
................
................
................` ],
  [ enemie, bitmap `
................
................
................
..........00....
...........0....
..0.........05..
...0........000.
....00000000....
....00000000....
....00000000....
....0.0..0.0....
....0.0..0.0....
................
................
................
................` ]
)

setSolids([ player, fence_vertical, fence_horizontal, fence_tl_corner, fence_tr_corner, fence_bl_corner, fence_br_corner ])

let level = 0;
const levels = [
  map`
chhhhhhhhhhhb
v...........v
v...........v
v..f........v
v...........v
v........f..v
v...........v
v....p......v
v...........v
v..f........v
v.......f...v
v...........v
nhhhhhhhhhhhm`,
  map `
chhhhhhhhhhhb
v..f........v
v...........v
v..f........v
v....ff..f..v
v...........v
v.....p.....v
v...........v
v...........v
v....f......v
v...........v
v.f......f..v
nhhhhhhhhhhhm`,
  map `
chhhhhhhhhhhb
v...........v
v.f.......f.v
v..f......f.v
v.....f.....v
v.........f.v
v......f....v
v..f..p.....v
v...........v
v..f.......fv
v........f..v
vffffff....fv
nhhhhhhhhhhhm`,
  map `
chhhhhhhhhhhb
v......ff...v
v.f.....chb.v
v....f..vfv.v
v..cb...vfv.v
v..nm...vfvfv
v.......vfv.v
vf....p.vfv.v
v.cb....vfv.v
v.vv....vfv.v
v.nm....vfv.v
vf...f......v
nhhhhhhhhhhhm`,
  map `
chhhhhhhhhhhb
vfffffffffffv
vfffffffffffv
vfffffffffffv
vfffffffffffv
vfffffffffffv
vffffchbffffv
vffffvpvffffv
vffffv.vffffv
vffffv.vffffv
v.hhhm.nhhh.v
v...........v
nhhhhhhhhhhhm`
]

setMap(levels[level])
setBackground(grass)

setPushables({
  [ player ]: []
})

const melody = tune`
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + C5^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + C5^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + D5^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + C5^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + E4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + E4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428 + A5-214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428 + A5-214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B4^214.28571428571428 + G5~214.28571428571428 + A5-214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428 + A5-214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428 + B5-214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + A4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A5-214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + A5-214.28571428571428 + F4^214.28571428571428 + F5~214.28571428571428,
214.28571428571428: C4/214.28571428571428 + B5-214.28571428571428 + G4^214.28571428571428 + G5~214.28571428571428`
const bark = tune `
500: C4/500 + D4-500 + E4^500,
15500`;

const playback = playTune(melody, Infinity)

addText("Get all BONES", { y: 1, color: color`0` });

onInput("w", () => {
  getFirst(player).y -= 1;
})
onInput("s", () => {
  getFirst(player).y += 1;
})
onInput("a", () => {
  getFirst(player).x -= 1;
})
onInput("d", () => {
  getFirst(player).x += 1;
})

afterInput(() => {
  //check to see if the player collects a bone
  const collect_tiles = tilesWith(player, bone);
  
  if(collect_tiles.length > 0){
    const x = collect_tiles[0][0].x;
    const y = collect_tiles[0][0].y;
    
    clearTile(x, y);
    addSprite(x, y, "p");
    playTune(bark)

    //get number of bones left
    const num_bones_left = tilesWith(bone).length;
    if(num_bones_left > 0){
      clearText();
      addText(num_bones_left + " bones left!", { y: 1, color: color`0` });
    }else{
      clearText();
      
      level += 1;
      const next_level = levels[level];
      
      if(next_level != undefined){
        addText("Next Level!!", { y: 1, color: color`0` });
        setMap(next_level);
      }else{
        addText("You WIN!!!", { y: 1, color: color`0` });
      }
    }
  }
});
